package org.zk.controller;

import com.google.gson.Gson;
import org.zk.entity.User;
import org.zk.service.PurseService;
import org.zk.service.UserService;
import org.zk.util.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping(value = "/purse")
public class PurseController {

    @Autowired
    private UserService userService;
    @Autowired
    private PurseService purseService;

    private Gson gson = new Gson();

    @ResponseBody
    @RequestMapping(value = "/recharge", method = {RequestMethod.POST}, produces = "text/plain;charset=utf-8")
    public String recharge(HttpServletRequest request, Float money) {

        Msg msg = new Msg(0);

        User user = (User) request.getSession().getAttribute("currentUser");

        if (user == null) {
            msg.setMessage("用户未登录");
            return gson.toJson(msg);
        }
        if(money>0){
            Integer rst = purseService.rechargePurse(user.getId(), money);
            if(rst!= null && rst > 0){
                msg.setCode(1);
                return gson.toJson(msg);
            }
        }
        return gson.toJson(msg);

    }



}
